package com.qiezikaka.kaka.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiezikaka.kaka.R;
import com.qiezikaka.kaka.adapter.FuZhuangDataListAdapter;
import com.qiezikaka.kaka.base.BaseFragment;
import com.qiezikaka.kaka.bean.AllBean;
import com.qiezikaka.kaka.bean.BannerInfo;
import com.qiezikaka.kaka.bean.FuZhuangInfo;
import com.qiezikaka.kaka.bean.HomeCateInfo;
import com.qiezikaka.kaka.bean.ShopInfo;
import com.qiezikaka.kaka.bean.UpdataVersionInfo;
import com.qiezikaka.kaka.databinding.FragementCateTabBinding;
import com.qiezikaka.kaka.ui.detail.DetailCameraFaceTakleActivity;
import com.qiezikaka.kaka.view.GridSpacingItemDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;


/**
 * 政策解读
 */
public class CateTabFragment extends BaseFragment<MainPresenter> implements MainContract.View, BaseQuickAdapter.OnItemClickListener {
    private MainPresenter presenter;

    // 是否是上拉加载更多获取数据
    private boolean hasLoadMore = false;
    private boolean isFristLoad = true;
    private int PAGE_NO = 1;
    private int PAGE_SIZE = 10;
    private FuZhuangDataListAdapter teachDataAdapter;
    private String tid;
    private com.qiezikaka.kaka.databinding.FragementCateTabBinding tabBinding;

    public static CateTabFragment newInstance(String type) {
        CateTabFragment newFragment = new CateTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }


    @Override
    protected View initView(ViewGroup container) {
        tabBinding = FragementCateTabBinding.inflate(getLayoutInflater(), container, false);
//        View view = LayoutInflater.from(mContext).inflate(R.layout.fragement_cate_tab, container, false);
        return tabBinding.getRoot();
    }

    @Override
    protected void loadData() {
        Bundle bundle = getArguments();
        tid = bundle.getString("id");
        initData();
        initRecycleView();
    }

    @Override
    public MainPresenter getPresenter() {
        if (presenter == null) {
            presenter = new MainPresenter(getActivity(), this);
        }
        return presenter;
    }

    private void initData() {
        presenter.getFuZhuanList(tid,PAGE_NO+"",PAGE_SIZE+"","");
    }

    private void initRecycleView() {
        tabBinding.refreshLayout.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
//        tabBinding.rvRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        tabBinding.rvRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        tabBinding.rvRecyclerView.addItemDecoration(new GridSpacingItemDecoration(5, 20, false));

        tabBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                PAGE_NO = 1;
                presenter.getFuZhuanList(tid,PAGE_NO+"",PAGE_SIZE+"","");
                hasLoadMore = false;

            }
        });

        tabBinding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                ++PAGE_NO;
                presenter.getFuZhuanList(tid,PAGE_NO+"",PAGE_SIZE+"","");
                hasLoadMore = true;
            }
        });
    }


    @Override
    public void onBannerList(BannerInfo info) {

    }

    @Override
    public void onHomeCate(HomeCateInfo info) {

    }

    @Override
    public void onFuZhuanList(FuZhuangInfo info) {
        tabBinding.refreshLayout.finishLoadMore();
        tabBinding.refreshLayout.finishRefresh();
        tabBinding.refreshLayout.resetNoMoreData();//setNoMoreData(false);
        switch (info.getStatus()) {
            case 200:
                FuZhuangInfo.DataBean infoData = info.getData();
                if (infoData != null) {
                    int page = infoData.getPage();
                    if (isFristLoad) {
                        List<FuZhuangInfo.DataBean.ListBean> list = infoData.getList();
                        teachDataAdapter = new FuZhuangDataListAdapter(R.layout.item_fuzhuang, list);
                        tabBinding.rvRecyclerView.setAdapter(teachDataAdapter);
                        teachDataAdapter.setOnItemClickListener(this::onItemClick);
                        isFristLoad = false;

                    } else {
                        if (!hasLoadMore) { //上拉加载
                            List<FuZhuangInfo.DataBean.ListBean> dataList = infoData.getList();
                            teachDataAdapter.replaceData(dataList);

                        } else { //下拉刷新
                            if (PAGE_NO <= page) {
                                //说明有值
                                List<FuZhuangInfo.DataBean.ListBean> dataList = infoData.getList();
                                teachDataAdapter.addData(dataList);
                            }
                        }
                    }
                    List<FuZhuangInfo.DataBean.ListBean> listBeanList = teachDataAdapter.getData();
                    if (listBeanList != null && listBeanList.size() != 0) {
                        tabBinding.rvRecyclerView.setVisibility(View.VISIBLE);
                        tabBinding.empty.clEmpty.setVisibility(View.GONE);
                    } else {
                        tabBinding.rvRecyclerView.setVisibility(View.GONE);
                        tabBinding.empty.clEmpty.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case 422:
                showToast(info.getMessage());
                break;
            default:
                break;
        }
    }

    @Override
    public void onFuZhuanHotList(FuZhuangInfo info) {

    }

    @Override
    public void onFailer(Throwable throwable) {
        tabBinding.refreshLayout.finishLoadMore();
        tabBinding.refreshLayout.finishRefresh();
        tabBinding.refreshLayout.resetNoMoreData();//setNoMoreData(false);
    }

    @Override
    public void onUpdataApp(UpdataVersionInfo InfoData) {

    }

    @Override
    public void onGetShop(ShopInfo info) {

    }

    @Override
    public void onBindShop(AllBean info) {

    }

    @Override
    public void onIsBind(AllBean info) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<FuZhuangInfo.DataBean.ListBean> list = adapter.getData();
        if(list != null && list.size() != 0){
            FuZhuangInfo.DataBean.ListBean listBean = list.get(position);
            if(listBean != null){
                String id = listBean.getId();
                String thumb = listBean.getThumb();
                Intent mIntent = new Intent(getActivity(), DetailCameraFaceTakleActivity.class);
                mIntent.putExtra("pid",id);
                startActivity(mIntent);
            }
        }
    }
}
